﻿using WeDonekRpc.Helper;
using WeDonekRpc.HttpApiGateway.Interface;
using WeDonekRpc.HttpApiGateway.Model;

namespace WeDonekRpc.HttpApiGateway
{
    public static class LingHelper
    {
        public static void CheckAccredit ( this IApiService service, ApiAccreditSet accredit )
        {
            if ( !accredit.IsAccredit )
            {
                return;
            }
            else if ( service.UserState == null )
            {
                throw new ErrorException("accredit.unauthorized");
            }
            else if ( !accredit.Power.IsNull() && !service.UserState.CheckPower(accredit.Power) )
            {
                throw new ErrorException("accredit.no.power");
            }
        }
        public static void CheckAccredit ( this IApiService service )
        {
            if ( service.UserState == null )
            {
                throw new ErrorException("accredit.unauthorized");
            }
        }
        public static void CheckAccredit ( this IApiService service, string[] prowers )
        {
            if ( service.UserState == null )
            {
                throw new ErrorException("accredit.unauthorized");
            }
            else if ( !prowers.IsNull() && !service.UserState.CheckPower(prowers) )
            {
                throw new ErrorException("accredit.no.power");
            }
        }
    }
}
